getwd()
## [1] "C:/Users/aldai/Documents"
setwd("D:/Documentos/Estadistica_Computacional")
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(VGAM)
## Loading required package: stats4
## Loading required package: splines
library(STAR)
## Loading required package: survival
## Loading required package: mgcv
## Loading required package: nlme
## This is mgcv 1.8-36. For overview type 'help("mgcv-package")'.
## 
## Attaching package: 'mgcv'
## The following object is masked from 'package:VGAM':
## 
##     s
## Loading required package: R2HTML
## Loading required package: gss
## Loading required package: codetools

1 Escribe una función en R que calcule la densidad de probabilidad de la variable aleatoria que se distribuye como:

\[ f_{x}(x)=\frac{1}{2a} e^{\frac{-|x|}{a}} \mathbb{I}_{(-\infty, \infty)}(x), a>0\]

genexp <- function(x,a){
  exp(-abs(x)/a)/(2*a)
}

2 Grafique la función de probabilidad de Laplace

x <- seq(-10,10,0.01)

data <- data.frame(x,genexp(x,1))

graph <- plot_ly(data, x= ~x, y= ~genexp(x,1), type= 'scatter', mode = 'lines')
layout(p=graph, title= "LAPLACE ", colorway= "Blue", yaxis=list(title= "fx"))

3 Utiliza el algoritmo explicado en clase para generar números aleatorios con distribución doble exponencial. Aplica este código para obtener una muestra aleatoria de tamaño 5000. Grafica el histograma correspondiente y sobre él incluye la curva de densidad teórica

exp_ale <- function(a){
  u_1 <- runif(1)
  u_2 <- runif(1)
  
  if(u_1<0.5){
    return(-a*log(u_2))
  } else {
    return(a*log(u_2))
  }
} 

dexp <- as.numeric(list())

for(i in 1:5000){
dexp[i] <- exp_ale(1)
}


hist(dexp, col=rainbow(10), main="Distribución Laplace Simulada ", xlab="M.Gen", ylab="Probabilidad", ylim = c(0,0.5), ,probability = T)
lines(x, genexp(x,1), col="coral2", lwd=3)

4 Realiza una prueba de hipótesis de bondad de ajuste para verificar que la muestra proviene de una distribución de Laplace.

#help(ks.test)
ks.test(dexp, plaplace(5000,0,1))
## 
##  Two-sample Kolmogorov-Smirnov test
## 
## data:  dexp and plaplace(5000, 0, 1)
## D = 0.8178, p-value = 0.3647
## alternative hypothesis: two-sided

El estradístico \(p\) mayor a 0.05 demuestra que no hay evidencia para rechazar la hipótesis. La muestra sí se distribuye como una Doble Exponencial.